<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/PHPExcel.php';

class Userlist extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('Users_m');
		$this->load->model('Agent_m');
		$this->load->model('Device_m');
		$this->load->model('Order_m');
		$this->load->model('Config_m');
		$this->load->model('Recharge_m');
		

	}
	
    /*会员列表*/
	public function userlists()
	{
		if(isset($_POST['key'])){
			$key = trim($_POST['key']);
			$nickname = "and a.nickname like '%{$key}%'";
			$this->db->like('nickname', $key);
		}else{
			$nickname = "";
		}
	    $total = $this->db->count_all_results('users');
		$page = $_POST['page'];
        $limit = $_POST['limit'];
        $offset = ($page - 1) * $limit;
        $sql = "select a.*,b.openid from  mifi_users as a, mifi_users_weixin as b where a.user_id = b.user_id $nickname order by a.user_id desc limit ".$offset.",".$limit."";
        $users = $this->db->query($sql)->result_array();
	    echo json_encode(['code'=>0,'count'=>$total,'data'=>$users]);
	}

	/*导出会员*/
	public function exportuser(){
		/*设置head头信息*/
	    Header("Content-Type:application/vnd.ms-excel;charset=UTF-8");
	    Header("Accept-Ranges:bytes");
	    Header("Content-Disposition:attachment;filename=".date('YmdHis').".xls");
	    Header("Pragma:no-cache");
	    Header("Expires:0");

	    $str = $str2 = null;
	    /*设置表格信息*/
	    $str.= "编号"."\t";
	    $str.= "会员昵称"."\t";
	    $str.= "openid"."\t";
	    $str.= "注册时间"."\t\r\n";
	    $info = iconv("UTF-8","GBK",$str);
	    echo $str;

        $sql = "select a.*,b.openid from  mifi_users as a, mifi_users_weixin as b where a.user_id = b.user_id order by user_id desc";
        $users = $this->db->query($sql)->result_array();
        if($users){
	        foreach ($users as $key => $val) {
				$str2.= $val['user_id']."\t";
				$str2.= $val['nickname']."\t";
				$str2.= $val['openid']."\t";
		        $str2.= $val['reg_time']."\t\r\n";
                $info = iconv("UTF-8","GBK",$str2);
			}
       	}
       	echo $str2;
	}

	//单条记录删除用户列表
	public function delusers(){
		$user_id = $_POST['user_id'];
		// $user_id = $this->input->post('user_id',true);
		$this->db->where('user_id', $user_id);
        $del = $this->db->delete('users'); 
        if($del){
           $this->db->where('user_id', $user_id);
           $dels = $this->db->delete('users_weixin'); 	
           if($dels){    	
              echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);
              exit;
           }else{
              echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
              exit;
           }
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
           exit;
        }
	}
	
	//批量删除用户记录
	public function delallusers(){
		$user_id = $_POST['user_id'];
		// $user_id = $this->input->post('user_id',true);
		$this->db->where_in('user_id',$user_id);
        $delall = $this->db->delete('users');
        if($delall){ 
           $this->db->where_in('user_id',$user_id);
           $delalls = $this->db->delete('users_weixin');
           if($delalls){
              echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);
              exit;
           }else{
           	  echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
              exit;
           }	
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
           exit;
        }
	}

	/*查询一级代理下的代理*/
	public function update_agents(){

		if ($_POST['agentone'] == 0) {
			echo _echo(1);exit;//选择的不是代理
		}else{
			$where['agent1'] = $_POST['agentone'];
			$deviceArr = $_POST['deviceArr'];
			foreach ($deviceArr as $key => $val) {
				$where['device_id'] = $val;
				$device_data[] = $this->Device_m->select($where);
			}
			//数组去重过后若只有一个表示只有一个一级代理，2个以上就是有多个一级代理
			$newArr = array();
			foreach($device_data as $key=>$val){
				//三维降二维
			    foreach($val as $k=>$v){
			      $newArr[] = $v['agent1'];
			    }
			}
			$count_arr = count(array_unique($newArr));
			if ($count_arr > 1) {
				echo _echo(2);exit;//表示有多个一级代理
			}else{
				$wheres = array('leader'=>$_POST['agentone']);
				$two_agent = $this->Agent_m->select($wheres);
				echo _echo($two_agent);exit;
			}
		}
	}
	/*修改哪些设备属于二级代理*/
	public function update_agenttwos(){
		$update1['agent1'] = $_POST['agentone'];
		$update2['agent2'] = $_POST['agenttwo'];
		$deviceArrs = $_POST['deviceArrs'];
		
		if ($update2['agent2'] == 0) {
			//只修改一级
			foreach ($deviceArrs as $key => $val) {
				$where['device_id'] = $val;
				$this->Device_m->update($where,$update1);
			}
		}else{
			//2级都要修改
			foreach ($deviceArrs as $key => $val) {
				$where['device_id'] = $val;
				$this->Device_m->update($where,$update1);
				$this->Device_m->update($where,$update2);
			}
		}
		echo _echo(1);exit;
	}

	
}
